/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.own.service;

import java.util.List;
import java.util.UUID;

import com.jeesite.common.datasource.DataSourceHolder;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.own.utils.MessageSaveUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.own.entity.OwnMedicineGrowerInformation;
import com.jeesite.modules.own.dao.OwnMedicineGrowerInformationDao;

import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * 种植户信息Service
 * @author F
 * @version 2019-09-27
 */
@Service
@Transactional(readOnly=true)
public class OwnMedicineGrowerInformationService extends CrudService<OwnMedicineGrowerInformationDao, OwnMedicineGrowerInformation> {

	@Autowired
	private OwnMedicineGrowerInformationDao ownMedicineGrowerInformationDao;

	@Autowired
	private MessageSaveUtils messageSaveUtils;
	/**
	 * 获取单条数据
	 * @param ownMedicineGrowerInformation
	 * @return
	 */
	@Override
	public OwnMedicineGrowerInformation get(OwnMedicineGrowerInformation ownMedicineGrowerInformation) {
		return super.get(ownMedicineGrowerInformation);
	}
	
	/**
	 * 查询分页数据
	 * @param ownMedicineGrowerInformation 查询条件
	 * @param ownMedicineGrowerInformation.page 分页对象
	 * @return
	 */
	@Override
	public Page<OwnMedicineGrowerInformation> findPage(OwnMedicineGrowerInformation ownMedicineGrowerInformation) {
		return super.findPage(ownMedicineGrowerInformation);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param ownMedicineGrowerInformation
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(OwnMedicineGrowerInformation ownMedicineGrowerInformation) {
		if(!isEmpty(UserUtils.getUser().getExtend())){

			ownMedicineGrowerInformation.setCreateCompany(UserUtils.getUser().getExtend().getExtendS1());

			String uuid = "";
			//如果是新数据则生成uuid
			if(StringUtils.isBlank(ownMedicineGrowerInformation.getId())){
				uuid = UUID.randomUUID().toString();
				ownMedicineGrowerInformation.setUuid(uuid);
			}else{
				//非新数据，直接获取uuid
				uuid = ownMedicineGrowerInformation.getUuid();
			}
			String txid = messageSaveUtils.getTxid(uuid, "种植户信息表");
			ownMedicineGrowerInformation.setTxid(txid);
		}

		super.save(ownMedicineGrowerInformation);
		DataSourceHolder.setDataSourceName("ds2");
		super.save(ownMedicineGrowerInformation);
		DataSourceHolder.clearDataSourceName();
	}
	
	/**
	 * 更新状态
	 * @param ownMedicineGrowerInformation
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(OwnMedicineGrowerInformation ownMedicineGrowerInformation) {
		super.updateStatus(ownMedicineGrowerInformation);
	}
	
	/**
	 * 删除数据
	 * @param ownMedicineGrowerInformation
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(OwnMedicineGrowerInformation ownMedicineGrowerInformation) {
		super.delete(ownMedicineGrowerInformation);
	}


	public List<OwnMedicineGrowerInformation> findByPlantId(String plantId) {
		return ownMedicineGrowerInformationDao.findByPlantId(plantId);
	}
	
}